package com.hsurosy.hsuchain.nft.notice.domain.entity;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Maps;
import com.hsurosy.hsuchain.nft.datasource.domain.entity.BaseEntity;
import com.hsurosy.hsuchain.nft.notice.domain.constant.NoticeState;
import com.hsurosy.hsuchain.nft.notice.domain.constant.NoticeType;
import lombok.Data;

import java.util.Date;
import java.util.Map;

/**
 * 通知实体类
 *
 * @Author Hsu琛君珩
 * @Date 2024-10-05 16:51
 * @Description
 * @Version: v1.0.0
 */
@Data
public class Notice extends BaseEntity {

    /**
     * 通知标题
     */
    private String noticeTitle;

    /**
     * 通知内容
     */
    private String noticeContent;

    /**
     * 通知类型
     */
    private NoticeType noticeType;

    /**
     * 发送成功时间
     */
    private Date sendSuccessTime;

    /**
     * 接收地址
     */
    private String targetAddress;

    /**
     * 通知状态
     */
    private NoticeState state;

    /**
     * 重试次数
     */
    private int retryTimes;

    /**
     * 扩展信息
     */
    private String extendInfo;

    /**
     * 添加扩展信息
     *
     * @param key 扩展信息的键
     * @param value 扩展信息的值
     */
    public void addExtendInfo(String key, String value) {
        Map<String, String> extendInfoMap;
        if (extendInfo == null) {
            extendInfoMap = Maps.newHashMapWithExpectedSize(1);
        } else {
            extendInfoMap = JSON.parseObject(this.extendInfo, Map.class);
        }
        extendInfoMap.put(key, value);
        this.extendInfo = JSON.toJSONString(extendInfoMap);
    }

    /**
     * 通知实体的构建器类
     */
    public static class Builder {

        /**
         * 通知标题
         */
        private String noticeTitle;

        /**
         * 通知内容
         */
        private String noticeContent;

        /**
         * 通知类型
         */
        private NoticeType noticeType;

        /**
         * 发送成功时间
         */
        private Date sendSuccessTime;

        /**
         * 接收地址
         */
        private String targetAddress;

        /**
         * 通知状态
         */
        private NoticeState state;

        /**
         * 扩展信息
         */
        private String extendInfo;

        /**
         * 设置通知标题
         *
         * @param noticeTitle 通知标题
         * @return 构建器对象
         */
        public Builder noticeTitle(String noticeTitle) {
            this.noticeTitle = noticeTitle;
            return this;
        }

        /**
         * 设置通知内容
         *
         * @param noticeContent 通知内容
         * @return 构建器对象
         */
        public Builder noticeContent(String noticeContent) {
            this.noticeContent = noticeContent;
            return this;
        }

        /**
         * 设置通知类型
         *
         * @param noticeType 通知类型
         * @return 构建器对象
         */
        public Builder noticeType(NoticeType noticeType) {
            this.noticeType = noticeType;
            return this;
        }

        /**
         * 设置发送成功时间
         *
         * @param sendSuccessTime 发送成功时间
         * @return 构建器对象
         */
        public Builder sendSuccessTime(Date sendSuccessTime) {
            this.sendSuccessTime = sendSuccessTime;
            return this;
        }

        /**
         * 设置接收地址
         *
         * @param targetAddress 接收地址
         * @return 构建器对象
         */
        public Builder targetAddress(String targetAddress) {
            this.targetAddress = targetAddress;
            return this;
        }

        /**
         * 设置通知状态
         *
         * @param state 通知状态
         * @return 构建器对象
         */
        public Builder state(NoticeState state) {
            this.state = state;
            return this;
        }

        /**
         * 设置扩展信息
         *
         * @param extendInfo 扩展信息
         * @return 构建器对象
         */
        public Builder extendInfo(String extendInfo) {
            this.extendInfo = extendInfo;
            return this;
        }

        /**
         * 构建并返回一个通知对象
         *
         * @return 构建好的通知对象
         */
        public Notice build() {
            Notice notice = new Notice();
            notice.noticeTitle = this.noticeTitle;
            notice.noticeContent = this.noticeContent;
            notice.noticeType = this.noticeType;
            notice.sendSuccessTime = this.sendSuccessTime;
            notice.targetAddress = this.targetAddress;
            notice.state = this.state;
            notice.extendInfo = this.extendInfo;
            return notice;
        }
    }

    /**
     * 获取构建器对象
     *
     * @return 构建器对象
     */
    public static Builder builder() {
        return new Builder();
    }
}